﻿
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System;

namespace RATSharp.Property
{
    /// <summary>
    /// Property enum which identifies properties.
    /// </summary>
    public enum PROPERTY { 
        /// <summary>
        /// Property url.
        /// </summary>
        URL = 0,
        /// <summary>
        /// Property port.
        /// </summary>
        PORT = 1,
        /// <summary>
        /// Property username.
        /// </summary>
        USERNAME = 2,
        /// <summary>
        /// Property password.
        /// </summary>
        PASSWORD = 3,
        /// <summary>
        /// Property path.
        /// </summary>
        PATH = 4
    };

    /// <summary>
    /// Class for loading and storing App properties.
    /// </summary>
    public static class PropertyLoader
    {
        /// <summary>
        /// event when some property changes, e.g. for reloading context.
        /// </summary>
        public static event PropertyChangeEventHandler PropertiesChangeEvent;
        private static string KEY_PROPERTY_URL = "url";
        private static string KEY_PROPERTY_PORT = "port";
        private static string KEY_PROPERTY_USERNAME = "username";
        private static string KEY_PROPERTY_PASSWORD = "password";
        private static string KEY_PROPERTY_PATH = "path";

        /// <summary>
        /// Load property from App.conf and parse them into Properties object 
        /// </summary>
        /// <returns>parsed properties</returns>
        public static Properties resolve()
        {
            string url = ConfigurationManager.AppSettings[KEY_PROPERTY_URL];
            string port = ConfigurationManager.AppSettings[KEY_PROPERTY_PORT];
            string username = ConfigurationManager.AppSettings[KEY_PROPERTY_USERNAME];
            string password = ConfigurationManager.AppSettings[KEY_PROPERTY_PASSWORD];
            string path = ConfigurationManager.AppSettings[KEY_PROPERTY_PATH];

            int portAsInt;
            try
            {
                portAsInt = Convert.ToInt32(port);
                UriBuilder builder = new UriBuilder("https", url, portAsInt, path);
 
                string parsedUrl = builder.ToString();
                return new Properties(parsedUrl, portAsInt, username, password, path);
            }
            catch (Exception ex)
            {
                // TODO: trace
                return null;
            }

        }

        /// <summary>
        /// Store new property into App.conf file and notify all listeners about this change.
        /// </summary>
        /// <param name="propertyType">what property</param>
        /// <param name="value">property value</param>
        /// <param name="source">event source</param>
        public static void persist(PROPERTY propertyType, string value, object source)
        {
            System.Configuration.Configuration config =
     ConfigurationManager.OpenExeConfiguration
                (ConfigurationUserLevel.None);



            config.AppSettings.Settings.Add("ModificationDate",
                   DateTime.Now.ToLongTimeString() + " ");

            // Save the changes in App.config file.
            config.Save(ConfigurationSaveMode.Modified);

            // Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings");


            switch (propertyType)
            {
                case PROPERTY.USERNAME:
                    config.AppSettings.Settings.Remove(KEY_PROPERTY_USERNAME);
                    config.AppSettings.Settings.Add(KEY_PROPERTY_USERNAME,
                 value);
                    break;
                case PROPERTY.PATH:
                    config.AppSettings.Settings.Remove(KEY_PROPERTY_PATH);
                    config.AppSettings.Settings.Add(KEY_PROPERTY_PATH,
                 value);
                    break;
                case PROPERTY.PASSWORD:
                    config.AppSettings.Settings.Remove(KEY_PROPERTY_PASSWORD);
                    config.AppSettings.Settings.Add(KEY_PROPERTY_PASSWORD, value);
                    break;
                case PROPERTY.PORT:
                    config.AppSettings.Settings.Remove(KEY_PROPERTY_PORT);
                    config.AppSettings.Settings.Add(KEY_PROPERTY_PORT, value);
                    break;
                case PROPERTY.URL:
                    config.AppSettings.Settings.Remove(KEY_PROPERTY_URL);
                    config.AppSettings.Settings.Add(KEY_PROPERTY_URL, value);
                    break;
                default:
                    break;
            }

            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("appSettings");

            if (PropertiesChangeEvent != null)
            {
                PropertiesChangeEvent(source, resolve());
            }
        }

    }

}
